//
//  LZRemindView.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/12/12.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZRemindView.h"
#import "Masonry.h"

@interface LZRemindView ()

@property (nonatomic, weak) UIView *bgView;
@property (nonatomic, weak) UILabel *label;
@property (nonatomic, weak) UIImageView *imageView;

@end

@implementation LZRemindView


+ (void)initWithImage:(UIImage*)aImage andContent:(NSString*)content {
    LZRemindView *aView = [[LZRemindView alloc] initWithFrame:CGRectZero];
    [aView setImage:aImage andContent:content];
    [[UIApplication sharedApplication].keyWindow addSubview:aView];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
    if (self) {
        [self configSubviews];
    }
    return self;
}

- (void)configSubviews {
    
    UIView *view = [[UIView alloc] init];
    view.layer.cornerRadius = 4;
    view.layer.masksToBounds = YES;
    view.backgroundColor = UIColorFromRGB(0x000000, 0.4);
    [self addSubview:view];
    self.bgView = view;
    
    UIImageView *imageV = [[UIImageView alloc] init];
    [self.bgView addSubview:imageV];
    self.imageView = imageV;
    
    UILabel *tempLabel = [[UILabel alloc] init];
    tempLabel.font = [UIFont systemFontOfSize:15*SCREEN_WIDTH/375.];
    tempLabel.textColor = UIColorFromRGB(0xffffff, 1);
    tempLabel.numberOfLines = 0;
    [self.bgView addSubview:tempLabel];
    self.label = tempLabel;
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.and.centerY.equalTo(self);
        make.width.mas_equalTo(182*SCREEN_WIDTH/375.);
    }];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView.mas_centerX);
        make.top.mas_equalTo(20);
    }];
    
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).offset(10);
        make.leading.equalTo(self.bgView).offset(20*SCREEN_WIDTH/375.);
        make.trailing.equalTo(self.bgView).offset(-20*SCREEN_WIDTH/375.);
    }];
}

- (void)setImage:(UIImage*)aImage andContent:(NSString*)content {
    self.imageView.image = aImage;
    
    // 调整行间距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:content];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    [paragraphStyle setLineSpacing:8];
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [content length])];
    self.label.attributedText = attributedString;
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.label.mas_bottom).offset(20);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self dismissView];
    });
}

- (void)dismissView {
    self.alpha = 1;
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}



@end
